package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.ASRecordMapper;
import com.vehicle.pojo.ASRecord;
import com.vehicle.service.ASRecordService;
import com.vehicle.util.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ASRecordServiceImpl implements ASRecordService {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private ASRecordMapper asRecordMapper;
    @Override
    public DataGridView findASRecordPage(ASRecord asRecord) {
        DataGridView dataGridView = new DataGridView();
        Page page = PageHelper.startPage(asRecord.getPage(),asRecord.getLimit());
        List<ASRecord> asRecords = asRecordMapper.selectASRecord(asRecord);
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(asRecords);
        return dataGridView;
    }

    @Override
    public DataGridView findAllASRecord(ASRecord asRecord) {
        DataGridView dataGridView = new DataGridView();
        dataGridView.setData(asRecordMapper.selectASRecord(new ASRecord()));
        return dataGridView;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteASRecordByid(int asid) {
        asRecordMapper.updateStatusById(asid);
    }

    @Override
    public void deleteBatchASRecord(Integer[] asIds) {
        for(Integer asid:asIds){
            asRecordMapper.updateStatusById(asid);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addASRecord(ASRecord asRecord) {
        Date date=null;
        try {
            date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateUtil.processingTime(asRecord.getAstime(),12));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        asRecord.setNext_astime(date);
        asRecordMapper.addASRecord(asRecord);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateASRecord(ASRecord asRecord) {
        Date date=null;
        try {
            date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateUtil.processingTime(asRecord.getAstime(),12));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        asRecord.setNext_astime(date);
        asRecordMapper.updateASRecord(asRecord);
    }
}
